package ui;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.UIManager;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.awt.Rectangle;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;

public class BookListUI extends JFrame {
	private JTable table;
	private JPanel contentPane;
	private JButton button;

	public BookListUI(String[][] booklist) {
		setTitle("图书列表");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(450, 250, 550, 400);
		setLocationRelativeTo(null); // 居中显示

		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(15, 15, 15, 15));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 15));

		// 标题
		JPanel titlePanel = new JPanel();
		titlePanel.setBackground(new Color(70, 130, 180));
		contentPane.add(titlePanel, BorderLayout.NORTH);

		javax.swing.JLabel titleLabel = new javax.swing.JLabel("图书查询结果", SwingConstants.CENTER);
		titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 18));
		titleLabel.setForeground(Color.WHITE);
		titlePanel.add(titleLabel);

		// 表格
		String[] columnNames = { "书名", "作者名", "价格" };
		table = new JTable(booklist, columnNames);

		// 美化表格
		table.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		table.setRowHeight(25);
		table.setSelectionBackground(new Color(176, 224, 230));
		table.setSelectionForeground(Color.BLACK);

		// 美化表头
		JTableHeader header = table.getTableHeader();
		header.setFont(new Font("微软雅黑", Font.BOLD, 13));
		header.setBackground(new Color(240, 248, 255));
		header.setForeground(new Color(70, 130, 180));

		JScrollPane scroll = new JScrollPane(table);
		scroll.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.add(scroll, BorderLayout.CENTER);

		// 按钮面板
		JPanel buttonPanel = new JPanel();
		contentPane.add(buttonPanel, BorderLayout.SOUTH);

		button = new JButton("关闭");
		button.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		button.setBackground(new Color(70, 130, 180));
		button.setForeground(Color.WHITE);
		button.setFocusPainted(false);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				BookListUI.this.dispose();
			}
		});
		buttonPanel.add(button);

		this.setVisible(true);
	}
}
